package com.spt.core;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.quartz.SchedulerException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.spt.ws.dao.ITaskService;
@Component
public class InitSystem implements ApplicationContextAware {

    @Autowired
    private ITaskService taskService;
    
    @Autowired
    private ISysTableInit sysInitservice;
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //sysInitservice.initSysTables();
        try {
            sysInitservice.initSysTableByConfigFile();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        
        //Map<String, Object> permissionMap = applicationContext.getBeansWithAnnotation(TaskAnnotation.class);
        
        taskService.autoUpdate(getAllTasks(applicationContext));
        
        taskService.initTask();
        
        
        
        /*applicationContext.getBean("sdfs").getClass().getAnnotation(TaskAnnotation.class);
        
        
        List<String> allTask = new ArrayList<>();
        for (Object permissionObject : permissionMap.values()) {
            Class<? extends Object> permissionClass = permissionObject.getClass();
            TaskAnnotation annotation = permissionClass.getAnnotation(TaskAnnotation.class);
            
            if(annotation != null) {
                allTask.add(annotation.job_name());
            }
        }*/

    }
    
    private Map<String, List<String>> getAllTasks(ApplicationContext applicationContext) {
        String[] taskBeanNames = applicationContext.getBeanNamesForAnnotation(TaskAnnotation.class);
        Map<String, List<String>> allTasks = Maps.newHashMap();
        for(String beanName : taskBeanNames) {
            List<String> taskParam = Lists.newArrayListWithExpectedSize(6);
            TaskAnnotation annotation = applicationContext.getBean(beanName).getClass().getAnnotation(TaskAnnotation.class);
            //TASKID,GROUPID,CRON,JOBNAME,BEAN,LASTSTATUS
            if(annotation.task_id() == null || "".equals(annotation.task_id().trim())) continue;
            if(annotation.group_id() == null || "".equals(annotation.group_id().trim())) continue;
            taskParam.add(annotation.task_id());
            taskParam.add(annotation.group_id());
            taskParam.add(annotation.cron());
            taskParam.add(annotation.job_name());
            taskParam.add(beanName);
            taskParam.add(annotation.autoRun());
            allTasks.put(annotation.task_id(), taskParam);
        }
        return allTasks;
    }

}
